﻿package core.ui.controls
{
    import core.event.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class Scrollbar extends Sprite
    {
        private var size:int = 0;
        private var cs:Rectangle;
        private var holder:Sprite;
        private var home:Sprite;
        private var end:Sprite;
        private var drag:Sprite;
        private var bg:Sprite;
        private var dstart:Bitmap;
        private var dmiddle:Bitmap;
        private var dstop:Bitmap;
        private var dgrip:Bitmap;

        public function Scrollbar(param1:Sprite, param2:Rectangle)
        {
            this.holder = param1;
            this.cs = param2;
            param1.scrollRect = param2;
            param1.addEventListener(MouseEvent.MOUSE_WHEEL, this.MouseScroll, false, 1, true);
            this.size = param2.height;
            this.Render();
            return;
        }// end function

        public function SetContentHeight(height:int, height:Boolean = false) : void
        {
            this.cs.height = height;
            this.SetSize(this.size, height);
            return;
        }// end function

        public function SetWidth(height:int) : void
        {
            var _loc_2:* = this.holder.scrollRect;
            _loc_2.width = height;
            this.holder.scrollRect = _loc_2;
            return;
        }// end function

        public function SetSize(height:int, height:Boolean = false) : void
        {
            this.size = height;
            var _loc_3:* = this.holder.scrollRect;
            _loc_3.height = height > this.cs.height ? (this.cs.height) : (height);
            if (_loc_3.height + _loc_3.y > this.cs.height || _loc_3.height < this.cs.height && height)
            {
                _loc_3.y = this.cs.height - _loc_3.height;
            }
            this.holder.scrollRect = _loc_3;
            this.Recalculate();
            return;
        }// end function

        public function Recalculate() : void
        {
            if (this.cs.height > this.size)
            {
                if (this.visible == false)
                {
                    this.visible = true;
                    dispatchEvent(new ScrollEvent(ScrollEvent.APPEARANCE));
                }
                this.dmiddle.height = this.size - 42 - (this.cs.height - this.size) < 8 ? (8) : (this.size - 42 - (this.cs.height - this.size));
                this.dstop.y = this.dmiddle.y + this.dmiddle.height;
                this.dgrip.y = this.dmiddle.y + int(this.dmiddle.height / 2) - 3;
                this.end.y = this.size - 15;
                this.drag.y = Math.round(this.holder.scrollRect.y / (this.cs.height - this.size) * (this.size - this.drag.height - 32)) + 16;
            }
            else if (this.visible == true)
            {
                this.drag.y = 16;
                this.visible = false;
                dispatchEvent(new ScrollEvent(ScrollEvent.APPEARANCE));
            }
            this.bg.height = this.size;
            return;
        }// end function

        private function Render() : void
        {
            this.bg = new Sprite();
            this.bg.graphics.beginFill(15132390);
            this.bg.graphics.drawRect(0, 0, 16, 16);
            this.bg.graphics.endFill();
            this.bg.height = this.size;
            this.bg.alpha = 0.7;
            addChild(this.bg);
            this.home = new Sprite();
            this.home.addChild(new Bitmap(new ScrollbarUp()));
            this.home.addEventListener(MouseEvent.MOUSE_DOWN, this.UpClick, false, 1, true);
            var _loc_1:int = 1;
            this.home.y = 1;
            this.home.x = _loc_1;
            addChild(this.home);
            this.end = new Sprite();
            this.end.addChild(new Bitmap(new ScrollbarDown()));
            this.end.addEventListener(MouseEvent.MOUSE_DOWN, this.DownClick, false, 1, true);
            this.end.x = 1;
            this.end.y = this.size - 14;
            addChild(this.end);
            this.drag = new Sprite();
            this.drag.addEventListener(MouseEvent.MOUSE_DOWN, this.DragMouseDown, false, 1, true);
            this.drag.addEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, false, 1, true);
            this.drag.x = 1;
            this.drag.y = 14;
            this.dstart = new Bitmap(new ScrollbarVdrgTop());
            this.drag.addChild(this.dstart);
            this.dmiddle = new Bitmap(new ScrollbarVdrgMiddle());
            this.dmiddle.y = 5;
            this.drag.addChild(this.dmiddle);
            this.dstop = new Bitmap(new ScrollbarVdrgBottom());
            this.dstop.y = 15;
            this.drag.addChild(this.dstop);
            this.dgrip = new Bitmap(new ScrollbarVdrgGrip());
            this.dgrip.y = 6;
            this.dgrip.x = 5;
            this.drag.addChild(this.dgrip);
            addChild(this.drag);
            this.Recalculate();
            return;
        }// end function

        private function DragMove(... args) : void
        {
            args = this.holder.scrollRect;
            args.y = Math.round((this.cs.height - this.size) * ((this.drag.y - 16) / (this.height - this.drag.height - 32)));
            this.holder.scrollRect = args;
            return;
        }// end function

        public function ScrollTo(height:int) : void
        {
            var _loc_2:* = this.holder.scrollRect;
            if (height < _loc_2.y)
            {
                _loc_2.y = height;
                this.holder.scrollRect = _loc_2;
                this.Recalculate();
            }
            if (_loc_2.y + _loc_2.height < height)
            {
                _loc_2.y = height - _loc_2.height;
                this.holder.scrollRect = _loc_2;
                this.Recalculate();
            }
            return;
        }// end function

        private function MouseScroll(event:MouseEvent) : void
        {
            this.drag.y = this.drag.y - event.delta * 3;
            if (this.drag.y < 16)
            {
                this.drag.y = 16;
            }
            else if (this.drag.y > this.size - this.drag.height - 16)
            {
                this.drag.y = this.size - this.drag.height - 16;
            }
            this.DragMove();
            return;
        }// end function

        function UpClick(event:MouseEvent) : void
        {
            if (this.drag.y - 10 > 16)
            {
                this.drag.y = this.drag.y - 10;
            }
            else
            {
                this.drag.y = 16;
            }
            this.DragMove();
            return;
        }// end function

        function DownClick(event:MouseEvent) : void
        {
            if (this.drag.y + 10 < this.size - this.drag.height - 16)
            {
                this.drag.y = this.drag.y + 10;
            }
            else
            {
                this.drag.y = this.size - this.drag.height - 16;
            }
            this.DragMove();
            return;
        }// end function

        function DragMouseDown(event:MouseEvent) : void
        {
            this.drag.startDrag(false, new Rectangle(1, 16, 0, this.size - 32 - this.drag.height));
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.DragMove, false, 1, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, true, 1, true);
            return;
        }// end function

        function DragMouseUp(event:MouseEvent) : void
        {
            this.drag.stopDrag();
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.DragMove, true);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, true);
            return;
        }// end function

    }
}
